<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>DSF2FLAC: dsd_decimator.cpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">DSF2FLAC
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">dsd_decimator.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * dsf2flac - http://code.google.com/p/dsf2flac/</span>
<a name="l00003"></a>00003 <span class="comment"> * </span>
<a name="l00004"></a>00004 <span class="comment"> * A file conversion tool for translating dsf dsd audio files into</span>
<a name="l00005"></a>00005 <span class="comment"> * flac pcm audio files.</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Copyright (c) 2013 by respective authors.</span>
<a name="l00008"></a>00008 <span class="comment"> *</span>
<a name="l00009"></a>00009 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
<a name="l00010"></a>00010 <span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<a name="l00011"></a>00011 <span class="comment"> * the Free Software Foundation; either version 2 of the License, or</span>
<a name="l00012"></a>00012 <span class="comment"> * (at your option) any later version.</span>
<a name="l00013"></a>00013 <span class="comment"> *</span>
<a name="l00014"></a>00014 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
<a name="l00015"></a>00015 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00016"></a>00016 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<a name="l00017"></a>00017 <span class="comment"> * GNU General Public License for more details.</span>
<a name="l00018"></a>00018 <span class="comment"> *</span>
<a name="l00019"></a>00019 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
<a name="l00020"></a>00020 <span class="comment"> * along with this program; if not, write to the Free Software</span>
<a name="l00021"></a>00021 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</span>
<a name="l00022"></a>00022 <span class="comment"> *</span>
<a name="l00023"></a>00023 <span class="comment"> * </span>
<a name="l00024"></a>00024 <span class="comment"> * Acknowledgements</span>
<a name="l00025"></a>00025 <span class="comment"> * </span>
<a name="l00026"></a>00026 <span class="comment"> * Many thanks to the following authors and projects whose work has greatly</span>
<a name="l00027"></a>00027 <span class="comment"> * helped the development of this tool.</span>
<a name="l00028"></a>00028 <span class="comment"> * </span>
<a name="l00029"></a>00029 <span class="comment"> * </span>
<a name="l00030"></a>00030 <span class="comment"> * Sebastian Gesemann - dsd2pcm (http://code.google.com/p/dsd2pcm/)</span>
<a name="l00031"></a>00031 <span class="comment"> * SACD Ripper (http://code.google.com/p/sacd-ripper/)</span>
<a name="l00032"></a>00032 <span class="comment"> * Maxim V.Anisiutkin - foo_input_sacd (http://sourceforge.net/projects/sacddecoder/files/)</span>
<a name="l00033"></a>00033 <span class="comment"> * Vladislav Goncharov - foo_input_sacd_hq (http://vladgsound.wordpress.com)</span>
<a name="l00034"></a>00034 <span class="comment"> * Jesus R - www.sonore.us</span>
<a name="l00035"></a>00035 <span class="comment"> * </span>
<a name="l00036"></a>00036 <span class="comment"> */</span>
<a name="l00037"></a>00037  
<a name="l00049"></a>00049 <span class="preprocessor">#include &quot;dsd_decimator.h&quot;</span>
<a name="l00050"></a>00050 <span class="preprocessor">#include &lt;math.h&gt;</span>
<a name="l00051"></a>00051 <span class="preprocessor">#include &quot;filters.cpp&quot;</span>
<a name="l00052"></a>00052 
<a name="l00053"></a>00053 <span class="keyword">static</span> <span class="keywordtype">bool</span> lookupTableAllocated = <span class="keyword">false</span>;
<a name="l00054"></a>00054 
<a name="l00063"></a>00063 dsdDecimator::dsdDecimator(DsdSampleReader *r, dsf2flac_uint32 rate)
<a name="l00064"></a>00064 {
<a name="l00065"></a>00065         reader = r;
<a name="l00066"></a>00066         outputSampleRate = rate;
<a name="l00067"></a>00067         valid = <span class="keyword">true</span>;;
<a name="l00068"></a>00068         errorMsg = <span class="stringliteral">&quot;&quot;</span>;
<a name="l00069"></a>00069         
<a name="l00070"></a>00070         <span class="comment">// ratio of out to in sampling rates</span>
<a name="l00071"></a>00071         ratio = r-&gt;getSamplingFreq() / outputSampleRate;
<a name="l00072"></a>00072         <span class="comment">// how many bytes to skip after each out sample calc.</span>
<a name="l00073"></a>00073         nStep = ratio/8; 
<a name="l00074"></a>00074         
<a name="l00075"></a>00075         <span class="comment">// load the required filter into the lookuptable based on in and out sample rate</span>
<a name="l00076"></a>00076         <span class="keywordflow">if</span> (ratio == 8)
<a name="l00077"></a>00077                 initLookupTable(nCoefs_352,coefs_352,tzero_352);
<a name="l00078"></a>00078         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ratio == 16)
<a name="l00079"></a>00079                 initLookupTable(nCoefs_176,coefs_176,tzero_176);
<a name="l00080"></a>00080         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ratio == 32)
<a name="l00081"></a>00081                 initLookupTable(nCoefs_88,coefs_88,tzero_88);
<a name="l00082"></a>00082         <span class="keywordflow">else</span>
<a name="l00083"></a>00083         {
<a name="l00084"></a>00084                 valid = <span class="keyword">false</span>;
<a name="l00085"></a>00085                 errorMsg = <span class="stringliteral">&quot;Sorry, incompatible sample rate combination&quot;</span>;
<a name="l00086"></a>00086                 <span class="keywordflow">return</span>;
<a name="l00087"></a>00087         }
<a name="l00088"></a>00088         <span class="comment">// set the buffer to the length of the table if not long enough</span>
<a name="l00089"></a>00089         <span class="keywordflow">if</span> (nLookupTable &gt; reader-&gt;getBufferLength())
<a name="l00090"></a>00090                 reader-&gt;setBufferLength(nLookupTable);
<a name="l00091"></a>00091 }
<a name="l00092"></a>00092 
<a name="l00099"></a>00099 dsdDecimator::~dsdDecimator()
<a name="l00100"></a>00100 {
<a name="l00101"></a>00101         <span class="keywordflow">if</span> (lookupTableAllocated) {
<a name="l00102"></a>00102                 <span class="keywordflow">for</span> (dsf2flac_uint32 n=0; n&lt;nLookupTable; n++)
<a name="l00103"></a>00103                 {
<a name="l00104"></a>00104                         <span class="keyword">delete</span>[] lookupTable[n];
<a name="l00105"></a>00105                 }
<a name="l00106"></a>00106                 <span class="keyword">delete</span>[] lookupTable;
<a name="l00107"></a>00107         }
<a name="l00108"></a>00108 }
<a name="l00109"></a>00109 
<a name="l00116"></a>00116 dsf2flac_int64 dsdDecimator::getLength()
<a name="l00117"></a>00117 {
<a name="l00118"></a>00118         <span class="keywordflow">return</span> reader-&gt;getLength()/ratio;
<a name="l00119"></a>00119 }
<a name="l00126"></a>00126 dsf2flac_float64 dsdDecimator::getPosition()
<a name="l00127"></a>00127 {
<a name="l00128"></a>00128         <span class="keywordflow">return</span> (dsf2flac_float64)reader-&gt;getPosition(tzero)/ratio;
<a name="l00129"></a>00129 }
<a name="l00135"></a>00135 dsf2flac_float64 dsdDecimator::dsdDecimator::getFirstValidSample() {
<a name="l00136"></a>00136         <span class="keywordflow">return</span> (dsf2flac_float64)nLookupTable / nStep - (dsf2flac_float64)tzero / ratio;
<a name="l00137"></a>00137 }
<a name="l00143"></a>00143 dsf2flac_float64 dsdDecimator::dsdDecimator::getLastValidSample() {
<a name="l00144"></a>00144         <span class="keywordflow">return</span> (dsf2flac_float64)getLength() - (dsf2flac_float64)tzero / ratio;
<a name="l00145"></a>00145 }
<a name="l00152"></a>00152 dsf2flac_uint32 dsdDecimator::getOutputSampleRate()
<a name="l00153"></a>00153 {
<a name="l00154"></a>00154         <span class="keywordflow">return</span> outputSampleRate;
<a name="l00155"></a>00155 }
<a name="l00156"></a>00156 
<a name="l00163"></a>00163 <span class="keywordtype">bool</span> dsdDecimator::isValid()
<a name="l00164"></a>00164 {
<a name="l00165"></a>00165         <span class="keywordflow">return</span> valid;
<a name="l00166"></a>00166 }
<a name="l00167"></a>00167 
<a name="l00174"></a>00174 std::string dsdDecimator::getErrorMsg() {
<a name="l00175"></a>00175         <span class="keywordflow">return</span> errorMsg;
<a name="l00176"></a>00176 }
<a name="l00177"></a>00177 
<a name="l00184"></a>00184 <span class="keywordtype">void</span> dsdDecimator::initLookupTable(<span class="keyword">const</span> <span class="keywordtype">int</span> nCoefs,<span class="keyword">const</span> dsf2flac_float64* coefs,<span class="keyword">const</span> <span class="keywordtype">int</span> tz)
<a name="l00185"></a>00185 {
<a name="l00186"></a>00186         tzero = tz;
<a name="l00187"></a>00187         <span class="comment">// calc how big the lookup table is.</span>
<a name="l00188"></a>00188         nLookupTable = (nCoefs+7)/8;
<a name="l00189"></a>00189         <span class="comment">// allocate the table</span>
<a name="l00190"></a>00190         lookupTable = <span class="keyword">new</span> calc_type*[nLookupTable];
<a name="l00191"></a>00191         <span class="keywordflow">for</span> (dsf2flac_uint32 n=0; n&lt;nLookupTable; n++)
<a name="l00192"></a>00192         {
<a name="l00193"></a>00193                 lookupTable[n] = <span class="keyword">new</span> calc_type[256];
<a name="l00194"></a>00194                 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> m=0; m&lt;256; m++)
<a name="l00195"></a>00195                         lookupTable[n][m] = 0;
<a name="l00196"></a>00196         }
<a name="l00197"></a>00197         <span class="comment">// loop over each entry in the lookup table</span>
<a name="l00198"></a>00198         <span class="keywordflow">for</span> (dsf2flac_uint32 t=0; t&lt;nLookupTable; t++) {
<a name="l00199"></a>00199                 <span class="comment">// how many samples from the filter are spanned in this entry</span>
<a name="l00200"></a>00200                 <span class="keywordtype">int</span> k = nCoefs - t*8;
<a name="l00201"></a>00201                 <span class="keywordflow">if</span> (k&gt;8) k=8;
<a name="l00202"></a>00202                 <span class="comment">// loop over all possible 8bit dsd sequences</span>
<a name="l00203"></a>00203                 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> dsdSeq=0; dsdSeq&lt;256; ++dsdSeq) {
<a name="l00204"></a>00204                         dsf2flac_float64 acc = 0.0;
<a name="l00205"></a>00205                         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> bit=0; bit&lt;k; bit++) {
<a name="l00206"></a>00206                                 dsf2flac_float64 val;
<a name="l00207"></a>00207                                 <span class="keywordflow">if</span> (reader-&gt;msbIsPlayedFirst()) {
<a name="l00208"></a>00208                                         val = -1 + 2*(dsf2flac_float64) !!( dsdSeq &amp; (1&lt;&lt;(7-bit)) );
<a name="l00209"></a>00209                                 } <span class="keywordflow">else</span> {
<a name="l00210"></a>00210                                         val = -1 + 2*(dsf2flac_float64) !!( dsdSeq &amp; (1&lt;&lt;(bit)) );
<a name="l00211"></a>00211                                 }
<a name="l00212"></a>00212                                 acc += val * coefs[t*8+bit];
<a name="l00213"></a>00213                         }
<a name="l00214"></a>00214                         lookupTable[t][dsdSeq] = (calc_type) acc;
<a name="l00215"></a>00215                 }
<a name="l00216"></a>00216         }
<a name="l00217"></a>00217         lookupTableAllocated = <span class="keyword">true</span>;
<a name="l00218"></a>00218 }
<a name="l00219"></a>00219 
<a name="l00220"></a>00220 
<a name="l00242"></a>00242 <span class="keyword">template</span>&lt;&gt; <span class="keywordtype">void</span> dsdDecimator::getSamples(<span class="keywordtype">short</span> <span class="keywordtype">int</span> *buffer, dsf2flac_uint32 bufferLen, dsf2flac_float64 scale, dsf2flac_float64 tpdfDitherPeakAmplitude)
<a name="l00243"></a>00243 {
<a name="l00244"></a>00244         getSamplesInternal(buffer,bufferLen,scale,tpdfDitherPeakAmplitude,<span class="keyword">true</span>);
<a name="l00245"></a>00245 }
<a name="l00246"></a>00246 <span class="keyword">template</span>&lt;&gt; <span class="keywordtype">void</span> dsdDecimator::getSamples(<span class="keywordtype">int</span> *buffer, dsf2flac_uint32 bufferLen, dsf2flac_float64 scale, dsf2flac_float64 tpdfDitherPeakAmplitude)
<a name="l00247"></a>00247 {
<a name="l00248"></a>00248         getSamplesInternal(buffer,bufferLen,scale,tpdfDitherPeakAmplitude,<span class="keyword">true</span>);
<a name="l00249"></a>00249 }
<a name="l00250"></a>00250 <span class="keyword">template</span>&lt;&gt; <span class="keywordtype">void</span> dsdDecimator::getSamples(<span class="keywordtype">long</span> <span class="keywordtype">int</span> *buffer, dsf2flac_uint32 bufferLen, dsf2flac_float64 scale, dsf2flac_float64 tpdfDitherPeakAmplitude)
<a name="l00251"></a>00251 {
<a name="l00252"></a>00252         getSamplesInternal(buffer,bufferLen,scale,tpdfDitherPeakAmplitude,<span class="keyword">true</span>);
<a name="l00253"></a>00253 }
<a name="l00254"></a>00254 <span class="keyword">template</span>&lt;&gt; <span class="keywordtype">void</span> dsdDecimator::getSamples(<span class="keywordtype">float</span> *buffer, dsf2flac_uint32 bufferLen, dsf2flac_float64 scale, dsf2flac_float64 tpdfDitherPeakAmplitude)
<a name="l00255"></a>00255 {
<a name="l00256"></a>00256         getSamplesInternal(buffer,bufferLen,scale,tpdfDitherPeakAmplitude,<span class="keyword">false</span>);
<a name="l00257"></a>00257 }
<a name="l00258"></a>00258 <span class="keyword">template</span>&lt;&gt; <span class="keywordtype">void</span> dsdDecimator::getSamples(<span class="keywordtype">double</span> *buffer, dsf2flac_uint32 bufferLen, dsf2flac_float64 scale, dsf2flac_float64 tpdfDitherPeakAmplitude)
<a name="l00259"></a>00259 {
<a name="l00260"></a>00260         getSamplesInternal(buffer,bufferLen,scale,tpdfDitherPeakAmplitude,<span class="keyword">false</span>);
<a name="l00261"></a>00261 }
<a name="l00262"></a>00262 
<a name="l00270"></a>00270 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> sampleType&gt; <span class="keywordtype">void</span> dsdDecimator::getSamplesInternal(sampleType *buffer, dsf2flac_uint32 bufferLen, dsf2flac_float64 scale, dsf2flac_float64 tpdfDitherPeakAmplitude, <span class="keywordtype">bool</span> roundToInt)
<a name="l00271"></a>00271 {
<a name="l00272"></a>00272         <span class="comment">// check the buffer seems sensible</span>
<a name="l00273"></a>00273         div_t d = div(bufferLen,getNumChannels());
<a name="l00274"></a>00274         <span class="keywordflow">if</span> (d.rem) {
<a name="l00275"></a>00275                 fputs(<span class="stringliteral">&quot;Buffer length is not a multiple of getNumChannels()&quot;</span>,stderr);
<a name="l00276"></a>00276                 exit(EXIT_FAILURE);
<a name="l00277"></a>00277         }
<a name="l00278"></a>00278         <span class="comment">// get the sample buffer</span>
<a name="l00279"></a>00279         boost::circular_buffer&lt;dsf2flac_uint8&gt;* buff = reader-&gt;getBuffer();
<a name="l00280"></a>00280         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;d.quot ; i++) {
<a name="l00281"></a>00281                 <span class="comment">// filter each chan in turn</span>
<a name="l00282"></a>00282                 <span class="keywordflow">for</span> (dsf2flac_uint32 c=0; c&lt;getNumChannels(); c++) {
<a name="l00283"></a>00283                         calc_type sum = 0.0;
<a name="l00284"></a>00284                         <span class="keywordflow">for</span> (dsf2flac_uint32 t=0; t&lt;nLookupTable; t++) {
<a name="l00285"></a>00285                                 dsf2flac_uint32 byte = (dsf2flac_uint32) buff[c][t] &amp; 0xFF;
<a name="l00286"></a>00286                                 sum += lookupTable[t][byte];
<a name="l00287"></a>00287                         }
<a name="l00288"></a>00288                         sum = sum*scale;
<a name="l00289"></a>00289                         <span class="comment">// dither before rounding/truncating</span>
<a name="l00290"></a>00290                         <span class="keywordflow">if</span> (tpdfDitherPeakAmplitude &gt; 0) {
<a name="l00291"></a>00291                                 <span class="comment">// TPDF dither</span>
<a name="l00292"></a>00292                                 calc_type rand1 = ((calc_type) rand()) / ((calc_type) RAND_MAX); <span class="comment">// rand value between 0 and 1</span>
<a name="l00293"></a>00293                                 calc_type rand2 = ((calc_type) rand()) / ((calc_type) RAND_MAX); <span class="comment">// rand value between 0 and 1</span>
<a name="l00294"></a>00294                                 sum = sum + (rand1-rand2)*tpdfDitherPeakAmplitude;
<a name="l00295"></a>00295                         }
<a name="l00296"></a>00296                         <span class="keywordflow">if</span> (roundToInt)
<a name="l00297"></a>00297                                 buffer[i*getNumChannels()+c] = <span class="keyword">static_cast&lt;</span>sampleType<span class="keyword">&gt;</span>(round(sum));
<a name="l00298"></a>00298                         <span class="keywordflow">else</span>
<a name="l00299"></a>00299                                 buffer[i*getNumChannels()+c] = <span class="keyword">static_cast&lt;</span>sampleType<span class="keyword">&gt;</span>(sum);
<a name="l00300"></a>00300                 }
<a name="l00301"></a>00301                 <span class="comment">// step the buffer</span>
<a name="l00302"></a>00302                 <span class="keywordflow">for</span> (dsf2flac_uint32 m=0; m&lt;nStep; m++)
<a name="l00303"></a>00303                         reader-&gt;step();
<a name="l00304"></a>00304         }
<a name="l00305"></a>00305 }
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Fri Sep 13 2013 14:42:01 for DSF2FLAC by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
